package genedit.gui.propertyDialogs;

import genedit.gui.GMainFrame;
import genedit.gui.exceptions.ElementNePostoji;
import genedit.gui.exceptions.ElementPostoji;
import genedit.gui.exceptions.RequiredFieldException;
import genedit.gui.propertyDialogs.AbstractDialog.StanjeDijaloga;
import genedit.model.graph.GraphElement;
import genedit.model.graph.classModel.GMethod;
import genedit.model.graph.classModel.GParameter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PanelDetailMethod extends AbstractPanelDetail {
	
	GraphElement element;
	GMethod method;
	AbstractDialog parent;
	
	private JLabel lblVisibility;
	private JComboBox cmbVisibility;
	private String[] visibility;
	
	private JLabel lblType;
	private JTextField txtType;
	
	private JLabel lblName;
	private JTextField txtName;
	
	private JLabel lblParameter;
	private JButton btnDodavanje;
	private JButton btnIzmena;
	private JButton btnBrisanje;
	
	private JTable table;
	private String[] tableColumns;
	private JPanel panTable;
	private DialogTableModel tableModel;
	
	
	public PanelDetailMethod(StanjeDijaloga mode, AbstractDialog par, GraphElement el, GMethod meth) {
		
		parent = par;
		element = el;
		method = meth;
		
		setLayout(new BorderLayout());

		Dimension lblDimension = new Dimension(150,20);
		
		tableColumns = new String[2];
		tableColumns[0] = GMainFrame.getInstance().getResourceBundle().getString("lblType");
		tableColumns[1] = GMainFrame.getInstance().getResourceBundle().getString("lblName");
	
		tableModel = new DialogTableModel(tableColumns, method.getParameteres());
		
		
		panTable = new JPanel(new BorderLayout());
		panTable.setBackground(Color.WHITE);
		table = new JTable(tableModel);
		table.setAutoCreateRowSorter(true);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		Box boxCentar = new Box(BoxLayout.Y_AXIS);
		
		JPanel panVisibility = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblVisibility = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblVisibility"));
		lblVisibility.setPreferredSize(lblDimension);
		visibility = new String [] {"private", "protected", "public"};
		
		cmbVisibility = new JComboBox(visibility);
		cmbVisibility.setEnabled(mode != StanjeDijaloga.BROWSE);
		panVisibility.add(lblVisibility);
		panVisibility.add(cmbVisibility);
		if (!(parent instanceof InterfaceModelDialog))
		boxCentar.add(panVisibility);
		
		JPanel panType = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblType = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblRetType"));
		lblType.setPreferredSize(lblDimension);
		txtType = new JTextField(10);
		txtType.setEnabled(mode != StanjeDijaloga.BROWSE);
		panType.add(lblType);
		panType.add(txtType);
		boxCentar.add(panType);
		

		JPanel panName = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblName = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblName"));
		lblName.setPreferredSize(lblDimension);
		txtName = new JTextField(10);
		txtName.setEnabled((mode != StanjeDijaloga.BROWSE));
		panName.add(lblName);
		panName.add(txtName);
		boxCentar.add(panName);

		
		JPanel panParameter = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblParameter = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblParameter"));
		lblParameter.setPreferredSize(lblDimension);
		panParameter.add(lblParameter);
		btnDodavanje = new JButton();
		btnDodavanje.setIcon(new ImageIcon("resources/icons/add_16.gif"));
		 
		
		btnDodavanje.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				PanelDetailParameter panelDetailDodavanje = new PanelDetailParameter(StanjeDijaloga.ADD);
				AddUpdateDialog addUpdateDialog = new AddUpdateDialog(parent, 
						GMainFrame.getInstance().getResourceBundle().getString("DodavanjeMetode"), StanjeDijaloga.ADD, panelDetailDodavanje);
				addUpdateDialog.setVisible(true);
				
				if (addUpdateDialog.getClosingMode() == AddUpdateDialog.OK) {
					
					GParameter novi = new GParameter(panelDetailDodavanje.getTxtName().getText(), 
							panelDetailDodavanje.getTxtType().getText());
					
					try {
						method.getParameteres().addParameter(novi);
						tableModel.fireTableDataChanged();
						int i = 0;
						for (String key : method.getParameteres().getParameterKeys()) {
							{
								if (key.equals(novi.getName()))
								break;
								else i++;
							}
						}
						table.changeSelection(i, 0, false, false);
					}catch (ElementPostoji e) {
						JOptionPane.showMessageDialog(parent, e.getMessage());
					}
					}
			}
		});
		btnDodavanje.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnDodavanje"));
		btnDodavanje.setMnemonic(KeyEvent.VK_N);

		
		
		 
		btnIzmena = new JButton();
		btnIzmena.setIcon(new ImageIcon("resources/icons/update_16.gif"));
		btnIzmena.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(table.getRowCount()!=0)
				{
				int i = table.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				{
					PanelDetailParameter panelDetailIzmena = new PanelDetailParameter(StanjeDijaloga.UPDATE);
					int row = table.getSelectedRow();
					String name = (String) table.getValueAt(row, 1);
					try {				
						panelDetailIzmena.getTxtName().setText(method.getParameteres().getParameter(name).getName());
						panelDetailIzmena.getTxtType().setText(method.getParameteres().getParameter(name).getType());	
					} catch (ElementNePostoji e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					AddUpdateDialog addUpdateDialog = new AddUpdateDialog(parent, 
						GMainFrame.getInstance().getResourceBundle().getString("IzmenaAtributa"), StanjeDijaloga.UPDATE, panelDetailIzmena);
				addUpdateDialog.setVisible(true);
				
				if (addUpdateDialog.getClosingMode() == addUpdateDialog.OK) {
					try {
							method.getParameteres().getParameter(name).setName(panelDetailIzmena.getTxtName().getText());
							method.getParameteres().getParameter(name).setType(panelDetailIzmena.getTxtType().getText());
					tableModel.fireTableDataChanged();
					table.changeSelection(row, 0, false, false);
					} catch (ElementNePostoji e) {
						JOptionPane.showMessageDialog(parent, e.getMessage());
					}
				
					
				}

				}
			}
			}
		});
		btnIzmena.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnIzmena"));
		btnIzmena.setMnemonic(KeyEvent.VK_I);
		
		btnBrisanje = new JButton();
		btnBrisanje.setIcon(new ImageIcon("resources/icons/remove_16.gif"));
		btnBrisanje.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(table.getRowCount()!=0)
				{
				int i = table.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				{
					
					int row = table.getSelectedRow();
					String name = (String) table.getValueAt(row, 1);
					try {
						method.getParameteres().removeParameter(method.getParameteres().getParameter(name));
						tableModel.fireTableDataChanged();
					} catch (ElementNePostoji e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}				
								
				}
			}
				
		});
		btnBrisanje.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnBrisanje"));
		btnBrisanje.setMnemonic(KeyEvent.VK_B);	

		
	
		panParameter.add(btnDodavanje);
		panParameter.add(btnIzmena);
		panParameter.add(btnBrisanje);
		boxCentar.add(panParameter);

		
		panTable.add(new JScrollPane(table), BorderLayout.CENTER);
		boxCentar.add(panTable);
		add(boxCentar,BorderLayout.CENTER);
		
	
	
	}
	




	public JComboBox getCmbVisibility() {
		return cmbVisibility;
	}





	public void setCmbVisibility(JComboBox cmbVisibility) {
		this.cmbVisibility = cmbVisibility;
	}





	public JTextField getTxtType() {
		return txtType;
	}


	public void setTxtType(JTextField txtType) {
		this.txtType = txtType;
	}





	public JTextField getTxtName() {
		return txtName;
	}





	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}





	public void proveriIspravnostPodataka() throws RequiredFieldException {
		
		if (txtType.getText().trim().equals("")) {
			txtType.setBackground(Color.RED);
			//ok = false;
			txtType.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent e) {
					
					
				}
				
				@Override
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.isActionKey() || e.getKeyCode()==KeyEvent.VK_ENTER || e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
						return;
					
					else
						txtType.setBackground(Color.WHITE);
			//		ok = true;
					
				}
			});
			RequiredFieldException rfe = new RequiredFieldException(
					);
			throw rfe;
		}
		
		if (txtName.getText().trim().equals("")) {
			txtName.setBackground(Color.RED);
		//	ok = false;
			txtName.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent e) {
					
					
				}
				
				@Override
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.isActionKey() || e.getKeyCode()==KeyEvent.VK_ENTER || e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
						return;
					
					else
						txtName.setBackground(Color.WHITE);
			//		ok = true;
					
				}
			});
			RequiredFieldException rfe = new RequiredFieldException(
					);
			throw rfe;
		}
	}
		

}

